﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class USSelection : ScriptableObject
{
    private static Dictionary<UnityEngine.Object, float> selectionList = new Dictionary<UnityEngine.Object, float>();

    public static void AddSelection(UnityEngine.Object selectedObject)
    {
        if (!IsInInternalSelection(selectedObject))
        {
            USEventBase key = selectedObject as USEventBase;
            if (key != null)
            {
                selectionList.Add(key, key.Firetime);
            }
            USObserverKeyframe keyframe = selectedObject as USObserverKeyframe;
            if (keyframe != null)
            {
                selectionList.Add(keyframe, keyframe.FireTime);
            }
            USInternalKeyframe keyframe2 = selectedObject as USInternalKeyframe;
            if (keyframe2 != null)
            {
                selectionList.Add(keyframe2, keyframe2.Time);
            }
        }
    }

    public static void ClearSelection()
    {
        selectionList.Clear();
    }

    public static void DragSelection(Vector2 delta)
    {
        float num = USControl.convertEventPanePositionToTime(delta.x);
        foreach (KeyValuePair<UnityEngine.Object, float> pair in selectionList)
        {
            USEventBase key = pair.Key as USEventBase;
            if (key != null)
            {
                key.Firetime = pair.Value + num;
            }
            USObserverKeyframe keyframe = pair.Key as USObserverKeyframe;
            if (keyframe != null)
            {
                keyframe.FireTime = pair.Value + num;
            }
            USInternalKeyframe keyframe2 = pair.Key as USInternalKeyframe;
            if (keyframe2 != null)
            {
                keyframe2.Time = pair.Value + num;
            }
        }
    }

    public static int GetNumOfSelections()
    {
        int num = 0;
        foreach (KeyValuePair<UnityEngine.Object, float> pair in selectionList)
        {
            if (pair.Key is USEventBase)
            {
                num++;
            }
            else
            {
                if (pair.Key is USObserverKeyframe)
                {
                    num++;
                    continue;
                }
                if (pair.Key is USInternalKeyframe)
                {
                    num++;
                }
            }
        }
        return num;
    }

    public static object GetSelection(int index)
    {
        int num = 0;
        foreach (KeyValuePair<UnityEngine.Object, float> pair in selectionList)
        {
            if (num == index)
            {
                return pair.Key;
            }
            num++;
        }
        return null;
    }

    private static bool IsInInternalSelection(UnityEngine.Object selectedObject)
    {
        foreach (KeyValuePair<UnityEngine.Object, float> pair in selectionList)
        {
            if (pair.Key.Equals(selectedObject))
            {
                return true;
            }
        }
        return false;
    }

    public static bool IsInUnitySelection(UnityEngine.Object selectedObject)
    {
        foreach (UnityEngine.Object obj2 in Selection.objects)
        {
            if (obj2.Equals(selectedObject))
            {
                return true;
            }
        }
        return false;
    }

    public static void RemoveSelection(UnityEngine.Object selectedObject)
    {
        if ((selectedObject != null) && selectionList.ContainsKey(selectedObject))
        {
            selectionList.Remove(selectedObject);
        }
    }

    public static void StartDragSelection()
    {
        Dictionary<UnityEngine.Object, float> dictionary = new Dictionary<UnityEngine.Object, float>();
        foreach (KeyValuePair<UnityEngine.Object, float> pair in selectionList)
        {
            USEventBase key = pair.Key as USEventBase;
            if (key != null)
            {
                dictionary.Add(key, key.Firetime);
            }
            USObserverKeyframe keyframe = pair.Key as USObserverKeyframe;
            if (keyframe != null)
            {
                dictionary.Add(keyframe, keyframe.FireTime);
            }
            USInternalKeyframe keyframe2 = pair.Key as USInternalKeyframe;
            if (keyframe2 != null)
            {
                dictionary.Add(keyframe2, keyframe2.Time);
            }
        }
        selectionList = dictionary;
        int index = 0;
        foreach (KeyValuePair<UnityEngine.Object, float> pair2 in selectionList)
        {
            index++;
            if (pair2.Key is USInternalKeyframe)
            {
                index++;
            }
        }
        UnityEngine.Object[] objectsToUndo = new UnityEngine.Object[index];
        index = 0;
        foreach (KeyValuePair<UnityEngine.Object, float> pair3 in selectionList)
        {
            objectsToUndo[index] = pair3.Key;
            index++;
            USInternalKeyframe keyframe3 = pair3.Key as USInternalKeyframe;
            if ((keyframe3 != null) && (keyframe3.curve != null))
            {
                objectsToUndo[index] = keyframe3.curve;
                index++;
            }
        }
        Undo.RegisterUndo(objectsToUndo, "Moved Keyframes");
    }

    public static void ToggleAddSelection(UnityEngine.Object selectedObject)
    {
        if (IsInInternalSelection(selectedObject))
        {
            RemoveSelection(selectedObject);
        }
        else
        {
            AddSelection(selectedObject);
        }
    }

    public static UnityEngine.Object[] Objects
    {
        get
        {
            UnityEngine.Object[] objArray = new UnityEngine.Object[selectionList.Count];
            int index = 0;
            foreach (KeyValuePair<UnityEngine.Object, float> pair in selectionList)
            {
                objArray[index] = pair.Key;
                index++;
            }
            return objArray;
        }
    }
}

